package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 千峰教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsCategoryService categoryService;

    @RequestMapping("/create")
    public Object create(@RequestBody GoodsAllinone one){
        Object res = goodsService.add(one);
        return res;
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        if(goods.getId() == null){
            return ResponseUtil.badArgument();
        }
        goodsService.delete(goods.getId());
        return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
         return goodsService.update(goodsAllinone);
    }

    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){
        List<CatVo> categoryList = categoryService.findCategoryList();

        List<Map<String,Object>> brandList = brandService.findBrandList();

        Map<String,Object> data = new HashMap<>();
        data.put("categoryList", categoryList);
        data.put("brandList", brandList);

        return ResponseUtil.ok(data);
    }

    @RequestMapping("/detail")
    public Object details(Integer id){
        Map<String,Object> data = goodsService.findById(id);
        return ResponseUtil.ok(data);
    }


    @RequestMapping("/list")
    public Object list(
            @RequestParam(name = "page" ,defaultValue = "1", required = false) Integer page ,
            @RequestParam(name = "limit" ,defaultValue = "20", required = false) Integer limit ,
            String goodSn ,
            String name ,
            @RequestParam(name = "sort" ,defaultValue = "add_time", required = false)  String sort ,
            @RequestParam(name = "order" ,defaultValue = "desc", required = false)  String order){

        //是否是品牌管理员
        Subject subject = SecurityUtils.getSubject();

        List<Integer> brandIds = null;
        if(subject.hasRole("品牌制造商") && !subject.hasRole("超级管理员")){
            //如果是品牌制造商， 查询所有的品牌id
            brandIds = brandService.findBrandIds();
        }


        // 分页查询
        IPage<DtsGoods> ipage = goodsService.findPage(page, limit, sort, order, name, goodSn, brandIds);

        Map<String,Object> data = new HashMap<>();
        data.put("total" , ipage.getTotal());
        data.put("items" , ipage.getRecords());

        return ResponseUtil.ok(data);
    }

}
